package com.mycity.dao;

import java.util.ArrayList;
import java.util.List;

import com.mycity.bean.User;
import com.mycity.mapper.UserRowMapper;


public class UserDao extends RDSDao{

	public User retrieveUserByEmail(String email, String password) {
		User foundUser = new User();
		Object params[] = { email, password };
	    List<User> users = this.getTemplate().query("SELECT userId, username, email, password, name, surname from User where email=? and password=?", params, new UserRowMapper());
	    
	    	    
	    if(users == null || users.isEmpty())
	    	return new User();
	    else
	    	foundUser = users.get(0);
    
	    return foundUser;
	}
	
	public User addUser(String username, String name, String surname, String email, String password) {
		User addedUser;
		Object params[] = { username, name, surname, email, password };
	    this.getTemplate().update("INSERT INTO User(username, name, surname, email, password) VALUES(?,?,?,?,?)", params);
	    addedUser = this.retrieveUserByEmail(email, password);
	    return addedUser;
	}
	
	public boolean emailExists(String email){
		Object params[] = { email };
	    List<User> users = this.getTemplate().query("SELECT userId, username, email, password, name, surname from User where email=?", params, new UserRowMapper());
	    if(users == null || users.isEmpty())
	    	return false;
	    else
	    	return true;
	}
	
	public boolean usernameExists(String username){
		Object params[] = { username };
		List<User> users = this.getTemplate().query("SELECT userId, username, email, password, name, surname from User where username=?", params, new UserRowMapper());
		if(users == null || users.isEmpty())
	    	return false;
	    else
	    	return true;
	}	
}
